<?php

    function viewPartyStock(){

        global $user;
        define("REQ_LEVEL",2);
        define("REQ_LEVEL2",4);
        if($user->level!= REQ_LEVEL && $user->level!=REQ_LEVEL2) {
            Page::$content=UN_AUTH;
         return; 
        }
        $_SESSION['userid']= $user->userid;
        $url = "./ajax/partystock.php";
        $baseLinkUrl = "agricare.php";
        $addParamE = "&page=editpartystock";
        $addParamD = "&page=delpartystock";
        $idName = "id";

        $str="<link rel='stylesheet' type='text/css' media='screen' href='./css/redmond/jquery-ui-1.7.1.custom.css' />
            <link rel='stylesheet' type='text/css' media='screen' href='./css/ui.jqgrid.css' />
            <script src='./js/jquery.js' type='text/javascript'></script>
            <script src='./js/i18n/grid.locale-en.js' type='text/javascript'></script>
            <script src='./js/jquery.jqGrid.min.js' type='text/javascript'></script>
            
            <script type='text/javascript'>
            jQuery(document).ready(function(){

            jQuery('#list').jqGrid({
                url:'{$url}',
                datatype: 'xml',
                mtype: 'GET',
                colNames:['SN','Date', 'Party Name','Product','No. of Case','-','-'],
                colModel :[ 
                {name:'id', index:'id', width:25,sortable:false}, 
                {name:'collected_date', index:'collected_date', width:50, align:'left',sorttype:'text'}, 
                {name:'party_name', index:'party_name', width:100, align:'left',sortable:false}, 
                {name:'name', index:'name', width:60, align:'left',sortable:false}, 
                {name:'no_of_case', index:'no_of_case', width:50, align:'left',sortable:false}, 
                {name:'a', index:'a', width:20, align:'center', sortable:false, formatter:'showlink', formatoptions:{baseLinkUrl:'{$baseLinkUrl}', addParam: '{$addParamE}', idName:'{$idName}'}}, 
                {name:'b', index:'b', width:20, align:'center', sortable:false, formatter:'showlink', formatoptions:{baseLinkUrl:'{$baseLinkUrl}', addParam: '{$addParamD}', idName:'{$idName}'}} 
                ],
                pager: '#pager',
                rowNum:8,
                    width:500,
                    height:175,
                    hidegrid:false,
                rowList:[10,20,30],
                sortname: 'collected_date',
                sortorder: 'DESC',
                viewrecords: true,
                imgpath: '',
                caption: 'Party Stock List'
            }); 
            }); 
            </script>
            <table id='list'></table> 
            <div id='pager'></div> 
            ";
            Page::$content = $str;

    }


    function addPartyStock(){
        
        global $form,$ctrl,$database,$user;
        define("REQ_LEVEL",2);
        define("REQ_LEVEL2",4);
        if($user->level!= REQ_LEVEL && $user->level!=REQ_LEVEL2) return UN_AUTH;
        
        $str;

        if(isset($_SESSION['addPartyStockSuccess'])){
            unset($_SESSON['addPartyStockSuccess']);
            $str.= "<div class='notice'>Data Entered Successfully !!</div>";
        }

        if(isset($_POST['add'])){

            //print_r($_POST);
            //die();
            $retval = $ctrl->addPartyStock($_POST);

            if($retval){
                $_SESSION['addPartyStockSuccess'] = true;
                header("Location:".$ctrl->referrer);				
            }
            else{
                $_SESSION['value_array'] = $_POST;
                $_SESSION['error_array'] = $form->getErrorArray();
                header("Location:".$ctrl->referrer);				
            }
        } else {

            Page::$jslink=array('js/ui.core.js','js/ui.datepicker.js');
            Page::$csslink=array('css/ui.all.css');
            $party = $database->getParty();

            $product = $database->getProduct();

            $str .="<div id='heading'><h3> Add Party Stock </h3> </div>
                <div><form method='POST' action='{$_SERVER['REQUEST_URI']}'>
                <div>Collected date : <input type='text' name='collected_date' id='datepicker' value=\"{$form->value('collected_date')}\" size='10' />{$form->error('collected_date')}</div>
                <div>Party : <select name='party_id'>";
            foreach($party as $key){

                $selected = "";
                if($key['id'] == $form->value('party_id'))
                    $selected = "selected";
                $str .="<option value='{$key['id']}' {$selected}>{$key['name']}</option>"; 
            }

                
            $str .="</select>{$form->error('party_id')}</div>
                <div>Product : <select name='product_id'>";
            foreach($product as $key){
                $selected = "";
                if($key['id'] == $form->value('product_id'))
                    $selected = "selected";
                $str .= "<option value='{$key['id']}' {$selected}>{$key['name']} ({$key['quantity']} {$key['unit_name']})</option>";
            }            
            $str .="</select>{$form->error('product_id')}</div>
                <div>No. of case : <input type='text' name='no_of_case' size='5' value=\"{$form->value('no_of_case')}\" />{$form->error('no_of_case')}</div>
                <div><input type='hidden' name='add'></div>
                <div><input type='submit' value='Add' name='submit'></div>      
                </form></div>
                <script type='text/javascript'>
                $(document).ready(function(){
                                            $('#datepicker').datepicker({dateFormat:'yy-mm-dd'});
                                            });
                </script>";
            return $str;
        }

        //return $str;

    }

    function editPartyStock(){
        
        global $form,$ctrl,$database,$user;
        define("REQ_LEVEL",2);
        define("REQ_LEVEL2",4);
        if($user->level!= REQ_LEVEL && $user->level!=REQ_LEVEL2) return UN_AUTH;
        $str;
        /*
        if(isset($_SESSION['editPartyStockSuccess'])){
            unset($_SESSON['editPartyStockSuccess']);
            $str.= "<div class='notice'>Data Update Successfully !!</div>";
        }*/

        if(isset($_POST['edit'])){

            //print_r($_POST);
            //die();
            $retval = $ctrl->editPartyStock($_POST);

            if($retval){
                //$_SESSION['editPartyStockSuccess'] = true;
                header("Location:agricare.php?page=viewpartystock");				
            }
            else{
                $_SESSION['value_array'] = $_POST;
                $_SESSION['error_array'] = $form->getErrorArray();
                header("Location:".$ctrl->referrer);				
            }
        } else {

            Page::$jslink=array('js/ui.core.js','js/ui.datepicker.js');
            Page::$csslink=array('css/ui.all.css');
            $party = $database->getParty();

            $product = $database->getProduct();
            $data = $database->getPartyStockById($_GET['id']);

            if(time()-strtotime($data['created_date']) > 43200)
                return "<div class='notice'>You can not edit this data because your time limit exceed 12 hr ask admin to edit it.</div>";
            $form->setValue('collected_date',$data['collected_date']);            
            $form->setValue('party_id',$data['party_id']);          
            $form->setValue('product_id',$data['product_id']);
            $form->setValue('no_of_case',$data['no_of_case']);          

            //print_r($data);
            //die();

            $str .="<div id ='heading'><h3> Edit Party Stock</h3> </div>
                <div><form method='POST' action='{$_SERVER['REQUEST_URI']}'>
                <div>Collected date : <input type='text' name='collected_date' id='datepicker' value=\"{$form->value('collected_date')}\" size='10' />{$form->error('collected_date')}</div>
                <div>Party : <select name='party_id'>";
            foreach($party as $key){

                $selected = "";
                if($key['id'] == $form->value('party_id'))
                    $selected = "selected";
                $str .="<option value='{$key['id']}' {$selected}>{$key['name']}</option>"; 
            }

                
            $str .="</select>{$form->error('party_id')}</div>
                <div>Product : <select name='product_id'>";
            foreach($product as $key){
                $selected = "";
                if($key['id'] == $form->value('product_id'))
                    $selected = "selected";
                $str .= "<option value='{$key['id']}' {$selected}>{$key['name']} ({$key['quantity']} {$key['unit_name']})</option>";
            }            
            $str .="</select>{$form->error('product_id')}</div>
                <div>No. of case : <input type='text' name='no_of_case' size='5' value=\"{$form->value('no_of_case')}\" />{$form->error('no_of_case')}</div>
                <div><input type='hidden' name='edit'></div>
                <div><input type='hidden' name='id' value='{$_GET['id']}'></div>
                <div><input type='submit' value='Edit' name='submit'></div>      
                </form></div>
                <script type='text/javascript'>
                $(document).ready(function(){
                                            $('#datepicker').datepicker({dateFormat:'yy-mm-dd'});
                                            });
                </script>";
            return $str;
        }

        //return $str;

    }


    function mViewPartyStock(){
        global $database,$user;
        define("REQ_LEVEL",MMR_LEVEL);
        define("REQ_LEVEL2",ADMIN_LEVEL);
        if($user->level!= REQ_LEVEL && $user->level!=REQ_LEVEL2){ 
            Page::$content=UN_AUTH;
         return; 
        }
        $usr = $database->getParty();

        $url = "./ajax/mpartystock.php";
        $baseLinkUrl = "agricare.php";
/*
        $str="<link rel='stylesheet' type='text/css' media='screen' href='./css/redmond/jquery-ui-1.7.1.custom.css' />
            <link rel='stylesheet' type='text/css' media='screen' href='./css/ui.jqgrid.css' />
            <script src='./js/jquery.js' type='text/javascript'></script>
            <script src='./js/i18n/grid.locale-en.js' type='text/javascript'></script>
            <script src='./js/jquery.jqGrid.min.js' type='text/javascript'></script>
            <script src='./js/jquery.chainedSelects.js'></script>            
            <script src='./js/mviewpartystock.js'></script>            
            <div><select name='headquater' id='headquater'><option value='0' selected>----select-----</option>";
            foreach($headquater as $key){
                $str .= "<option value='{$key['id']}'>{$key['name']}</option>";
            }
            
        $str .="</select></div><div><select name='party' id='party'></select></div>    
            <table id='list'></table> 
            <div id='pager'></div>";

            //echo $str."dfjsdjfksdfjs";
            //die();
            Page::$content = $str;
*/


        $str="<link rel='stylesheet' type='text/css' media='screen' href='./css/redmond/jquery-ui-1.7.1.custom.css' />
            <link rel='stylesheet' type='text/css' media='screen' href='./css/ui.jqgrid.css' />
            <script src='./js/jquery.js' type='text/javascript'></script>
            <script src='./js/i18n/grid.locale-en.js' type='text/javascript'></script>
            <script src='./js/jquery.jqGrid.min.js' type='text/javascript'></script>
            <script src='./js/jquery.chainedSelects.js'></script>            
            <script src='./js/mviewpartystock.js'></script>            
            <div id ='heading'> <h3> View Party Stock</h3></div><div>Party : <select name='party' id='party'><option value='0' selected>----select-----</option>";
            foreach($usr as $key){
                $str .= "<option value='{$key['id']}'>{$key['name']}</option>";
            }
            
        $str .="</select></div><div><input type='submit' value='<< Previous' name='prev' id='prev'><input type='submit' value='Next>>' name='next' id='next'></div><div id='display'><table id='list'></table><div id='pager'></div></div>";

        Page::$content = $str;
    }

?>
